<template>
  <el-dialog :title='titleFont' :visible.sync='isShow' width='700px' :lock-scroll=false :close-on-click-modal=false @close='clearForm("queryForm")' @open="openInitializtaion">
    <div v-loading="loading">
      <el-card class='type-info-box-card'>
        <el-form ref='queryForm' size='mini' :model="queryForm" label-width="140px" :rules="rules" label-position="top">
          <el-row :gutter="30">
            <el-col :md='12' :lg='12'>
              <el-form-item label='柜员号' prop='AGENTID'>
                <!-- <el-input v-model='queryForm.AGENTID' ></el-input> -->
                <el-select v-model='queryForm.AGENTID' filterable>
                  <el-option v-for='item in AGENTIDS' :key='item.AGENTNAME' :label='item.AGENTNAME' :value='item.AGENTID'>
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <!-- <el-col :md='12' :lg='12'>
              <el-form-item label='柜员姓名' prop='AGENTNAME'>
                <el-input v-model='queryForm.AGENTNAME'></el-input>
              </el-form-item>
            </el-col> -->
          </el-row>
          <el-row :gutter="30">
            <el-col :span='100'>
              <el-form-item label='渠道名称' prop='CHLCODE'>
                <el-checkbox-group v-model='queryForm.CHLCODE'>
                  <el-checkbox v-for='item in CHLNAMEOpions' :key='item.CHLCODE' :label='item.CHLCODE'>{{item.CHLNAME}}</el-checkbox>
                </el-checkbox-group>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-card>
      <div slot="footer" style="margin:10px 0;text-align:center">
        <el-button size="mini" @click="isShow=false">取消</el-button>
        <el-button type='primary' size='mini' @click="submit">确定</el-button>
      </div>
    </div>
  </el-dialog>
</template>

<script>
  import DateUtils from '@/CTManage/utils/DateUtils';
  import request from "@/common/utils/requestCT";
  import cloudTellerAdd from "@/message/CTManage/teller/cloud-teller-add";
  import selectTree from "@/components/selectTree2/";
  import getGlobalParams from '@/common/utils/getGlobalParams';
  import spinnerList from "@/message/spinnerDataQuery/spinner-data-query";
  import channelNameQuery from "@/message/shared/channel-name-query";
  import tellerChannelUpdate from "@/message/CTManage/teller-channel/teller-channel-update";
  import cloudTellerQuery from "@/message/CTManage/teller/cloud-teller-query";
  export default {
    components: {
      selectTree
    },
    data() {
      return {
        AGENTIDS:[],
        loading: false,
        titleFont: '柜员对应渠道',
        queryForm: { //表单数据
          AGENTID: '',
          AGENTNAME: '',
          CHLCODE: []
        },
        CHLNAMEOpions: [],
        rules: { //表单校验规则
          CHLCODE: [{
            required: true,
            message: "请选择渠道名称",
            trigger: "change"
          }],
          AGENTID: [{
            required: true,
            message: "请选择柜员",
            trigger: "change"
          }]
        }
      };
    },
    props: {
      option: Object
    },
    methods: {
          queryTeller(){
      
      let resBody = new cloudTellerQuery();
       resBody.data.branchNo = this.$store.state.user.branchId;
          request(resBody)
              .then(response => {
                if (response.SYS_HEAD.ReturnCode === "000000") {
                  this.loading = false;
                this.AGENTIDS = response.RSP_BODY.resultSet;
                 
                } else {
                  this.$message({
                    type: "error",
                    message: "请求失败，错误码:" + response.SYS_HEAD.ReturnCode + "，错误信息：" + response.SYS_HEAD.ReturnMessage
                  });
                }
              })

      // if (responseTellerQuery.SYS_HEAD.ReturnCode == "000000") {
      //   this.AGENTIDS = responseTellerQuery.RSP_BODY.resultSet;
      // }else {
      //   this.$message({
      //       type: "error",
      //       message: "请求失败，错误码:" + responseTellerQuery.SYS_HEAD.ReturnCode + "，错误信息：" + responseTellerQuery.SYS_HEAD.ReturnMessage
      //     });
      // }
    },
      // 获取渠道名称方法
      async channelNameQuery() {
        this.loading = true;
        this.tableData = [];
        this.tableTotal = 0;
        let resBody = new channelNameQuery();
        let response = await request(resBody);
        this.loading = false;
        if (response.SYS_HEAD.ReturnCode == "000000") {
          this.CHLNAMEOpions = response.RSP_BODY.resultSet;
          console.log(response)
        } else {
          this.$message({
            type: "error",
            message: "请求失败，错误码:" + response.SYS_HEAD.ReturnCode + "，错误信息：" + response.SYS_HEAD.ReturnMessage
          });
        }
      },
      openInitializtaion() {
        if (this.$refs["queryForm"]) {
          this.$refs["queryForm"].resetFields();
        }
        this.queryTeller()
        this.channelNameQuery();
        // console.log(this.option)
        this.titleFont = '柜员对应渠道管理';
        const {
          AGENTID,
          AGENTNAME,
          CHLCODE
        } = this.option.currentRow
        this.queryForm = {
          AGENTID,
          AGENTNAME,
          CHLCODE: CHLCODE ? CHLCODE.split('|') : []
        }
        // console.log('queryForm', this.queryForm)
      },
      checkChange(val) {
        let chlcode = []
        this.queryForm.CHLNAME.forEach(val => {
          this.CHLNAMEOpions.forEach(valObj => {
            if (valObj.CHLNAME == val) {
              chlcode.push(valObj)
            }
          })
        })
        const fn = (arr, key) => arr.map(arr => arr[key]);
        let codeStr = fn(chlcode, 'CHLCODE').join()
        console.log(codeStr)
      },
      submit() {
        //添加策略事件
        this.$refs["queryForm"].validate(valid => {
          if (valid) {
            this.loading = true;
            const resBody = new tellerChannelUpdate();
            const codeStr = this.queryForm.CHLCODE.toString().replace("\"","").replace("[","").replace("]","")
            resBody.data.AGENTID = this.queryForm.AGENTID;
            resBody.data.AGENTNAME = this.queryForm.AGENTNAME;
            resBody.data.OPERATE = "I"
            resBody.data.CHLCODELENGTH=this.queryForm.CHLCODE.length
            resBody.data.CHLCODE = codeStr;
            request(resBody)
              .then(response => {
                if (response.SYS_HEAD.ReturnCode === "000000") {
                  this.loading = false;
                  this.$message({
                    type: "success",
                    message: "柜员对应渠道新增成功!"
                  });
                  this.isShow = false;
                  this.$emit("refreshTable");
                } else {
                  this.$message({
                    type: "error",
                    message: "请求失败，错误码:" + response.SYS_HEAD.ReturnCode + "，错误信息：" + response.SYS_HEAD.ReturnMessage
                  });
                  this.loading = false;
                }
              })
              .catch(error => {
                this.loading = false;
                console.log(error);
              });
          }
        });
      },
      clearForm(item) {
        if (this.$refs.queryForm) {
          this.$refs.queryForm.resetFields();
          this.$refs.queryForm.clearValidate();
        }
      }
    },
    computed: {
      isShow: {
        get: function() {
          return this.option.isShow;
        },
        set: function(newValue) {
          this.option.isShow = newValue;
        }
      }
    },
    async created() {}
    // mounted() {
    //   window.addEventListener("scroll", this.handleScroll);
    // }
  };
</script>

<style lang="scss" scope>

</style>
